package com.aca.market.controller;


import com.aca.market.entity.Address;
import com.aca.market.service.IAddressService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aca
 * @since 2020-11-13
 */
@RestController
@RequestMapping("/address")
@Slf4j
public class AddressController {

    @Autowired
    IAddressService addressService;

    @PostMapping("/add")
    //-----应该移到service层去实现
    public String addNewAddress(@RequestBody Address address){
        //它设为默认就要把其他的取消
        if (address.getIsDefault()){
            addressService.update(new UpdateWrapper<Address>()
                    .eq("is_default",1).set("is_default",0));
        }
        if (addressService.save(address)) {
            log.info("~~~~~~~~" + address);
            return address.toString();
        } else {
            log.info("~~~~~~~~~~~~~~~~ err:" + address);
            return "err-addNewAddress";
        }
    }


}

